﻿using DAL;
using DAL.DBManagers;
using DAL.Models;
using System;
using System.Linq;

public partial class VesselTypeEdit : System.Web.UI.Page
{
    Guid RecordID = Guid.Empty;
    Guid VesselTypeID = Guid.Empty;
    VesselTypesManager manager = new VesselTypesManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = String.Empty;

        if (Guid.TryParse(Request.QueryString["id"], out RecordID))
        {
            if (!IsPostBack)
            {
                try
                {
                    var client = manager.Single(s => s.RecordID == RecordID);

                    txtName.Text = client.Name;
                    txtBeginDate.DateValue = client.BeginDate;
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            var CurrentUser = Session["User"] as UserHelper;

            Guid NewVesselTypeID = Guid.Empty;

            if (RecordID == Guid.Empty)
            {
                if (Guid.TryParse(Request.QueryString["sid"], out VesselTypeID))
                {
                    if (manager.Find(f => f.IsActive && f.VesselTypeID == VesselTypeID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                    {
                        throw new Exception("VesselType already exists with this date");
                    }

                    manager.Add(new VesselType
                    {
                        RecordID = Guid.NewGuid(),
                        VesselTypeID = VesselTypeID,
                        Name = txtName.Text,
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    });

                    NewVesselTypeID = VesselTypeID;
                }
                else
                {
                    var VesselType = new VesselType
                    {
                        RecordID = Guid.NewGuid(),
                        VesselTypeID = Guid.NewGuid(),
                        Name = txtName.Text,
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    };

                    manager.Add(VesselType);

                    NewVesselTypeID = VesselType.VesselTypeID;
                }
            }
            else
            {
                var VesselType = manager.Single(s => s.RecordID == RecordID);

                if (manager.Find(f => f.IsActive && f.VesselTypeID == VesselType.VesselTypeID && f.RecordID != RecordID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                {
                    throw new Exception("VesselType already exists with this date");
                }

                VesselType.Name = txtName.Text;
                VesselType.BeginDate = txtBeginDate.DateValue.Value;
                VesselType.LastModifyDate = DateTime.Now;
                VesselType.LastModifier = CurrentUser.UserID;

                NewVesselTypeID = VesselType.VesselTypeID;
            }

            manager.SaveChanges();
            manager.RepiarDates(NewVesselTypeID);

            if (VesselTypeID == Guid.Empty && RecordID == Guid.Empty)
            {
                Response.Redirect("vesseltypeview.aspx?m=liVesselType");
            }
            else
            {
                Response.Redirect(String.Format("~/vesseltypehistory.aspx?id={0}&m={1}", NewVesselTypeID, "liVesselType"));
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
}